package com.gohi.main.user.service.impl;


import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.TransactionException;

import com.gohi.core.service.impl.DefaultService;
import com.gohi.core.web.paging.PagingBean;
import com.gohi.main.user.hbm.ShopgooWxMember;
import com.gohi.main.user.service.IShopgooWxMemberService;
import com.gohi.main.util.Utils;

/**
 * <B>微信的用户映射表</B>
 * @author 肖得明<br>
 * 2015年4月13日10时34分27秒
 */
public class ShopgooWxMemberServiceImpl extends DefaultService<ShopgooWxMember, String> implements IShopgooWxMemberService {


	

	@Override
	public ShopgooWxMember saveOrUpdate(ShopgooWxMember bean) throws TransactionException {
		return this.getDAO().save(bean);
	}

	@Override
	public void deleteById(String id) throws TransactionException {
		this.getDAO().remove(new ShopgooWxMember(), id);
	}
	
	@Override
	public ShopgooWxMember getById(String id) throws TransactionException {
		return this.getDAO().getByPK(new ShopgooWxMember(), id);
	}

	@Override
	public List<ShopgooWxMember> select(ShopgooWxMember bean)
			throws TransactionException {
		StringBuffer hql = new StringBuffer("from ShopgooWxMember where 1=1 ");
		ArrayList<Object> objs = new ArrayList<Object>();
		if(bean!=null){
			            //主键openid
            if(!Utils.show(bean.getId()).equals("")){
                hql.append(" and id=?");
                objs.add(bean.getId());
            }
            //头像地址
            if(!Utils.show(bean.getHeadimg()).equals("")){
                hql.append(" and headimg=?");
                objs.add(bean.getHeadimg());
            }
            //昵称
            if(!Utils.show(bean.getNickname()).equals("")){
                hql.append(" and nickname=?");
                objs.add(bean.getNickname());
            }
            //对应的用户真实的用户ID
            if(!Utils.show(bean.getRealid()).equals("")){
                hql.append(" and realid=?");
                objs.add(bean.getRealid());
            }
            //经纬度
            if(!Utils.show(bean.getLng()).equals("")){
                hql.append(" and lng=?");
                objs.add(bean.getLng());
            }
            //经纬度
            if(!Utils.show(bean.getLat()).equals("")){
                hql.append(" and lat=?");
                objs.add(bean.getLat());
            }

		}
		return this.getDAO().findByHql(hql.toString(), objs.toArray());
	}

	@Override
	public List<ShopgooWxMember> select(ShopgooWxMember bean, PagingBean pb)
			throws TransactionException {
		StringBuffer hql = new StringBuffer("from ShopgooWxMember where 1=1 ");
		ArrayList<Object> objs = new ArrayList<Object>();
		if(bean!=null){
			            //主键openid
            if(!Utils.show(bean.getId()).equals("")){
                hql.append(" and id=?");
                objs.add(bean.getId());
            }
            //头像地址
            if(!Utils.show(bean.getHeadimg()).equals("")){
                hql.append(" and headimg=?");
                objs.add(bean.getHeadimg());
            }
            //昵称
            if(!Utils.show(bean.getNickname()).equals("")){
                hql.append(" and nickname=?");
                objs.add(bean.getNickname());
            }
            //对应的用户真实的用户ID
            if(!Utils.show(bean.getRealid()).equals("")){
                hql.append(" and realid=?");
                objs.add(bean.getRealid());
            }
            //经纬度
            if(!Utils.show(bean.getLng()).equals("")){
                hql.append(" and lng=?");
                objs.add(bean.getLng());
            }
            //经纬度
            if(!Utils.show(bean.getLat()).equals("")){
                hql.append(" and lat=?");
                objs.add(bean.getLat());
            }

		}
		return this.getDAO().findByHql(hql.toString(), objs.toArray(), pb);
	}

	@Override
	public int wxIsBind(String userid) {
		// TODO Auto-generated method stub
		String sql = "select  count(*) from shopgoo_wx_member where realid="+userid;
	//	System.err.println(sql);
		return this.getDAO().getJdbcTemplate().queryForInt(sql);
	}



}
